package org.example.gen;

import org.example.LandEntity;
import org.example.LogInterface;
import org.example.area.AreaDto;
import org.example.area.AreaList;
import org.example.tool.WordUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

public class SurveyExcelGen implements CallBack {
    private final LogInterface log;
    private final Map<String, List<LandEntity>> tempLands;
    private final Map<String,List<Map<String, Object>>> inspectionReportList;
    private  boolean isAll;

    public SurveyExcelGen(LogInterface log, Map<String, List<LandEntity>> tempLands, Map<String,List<Map<String, Object>>> inspectionReportList, boolean isAll) {
        this.log = log;
        this.tempLands = tempLands;
        this.isAll = isAll;
        this.inspectionReportList = inspectionReportList;

    }
    @Override
    public boolean doWork(String nf, String bxgsmc, String qhdm, String zwmc, String dirPath) {
        AreaList.getInstance();
        log.logInfo(String.format("开始生成%s-%s-%s-%s的调查表", nf,bxgsmc,qhdm,zwmc));
        AreaDto area = AreaList.getFullNameEntity(qhdm);
        if(inspectionReportList.get(qhdm+zwmc) == null){
            log.logWarn(String.format("%s-%s-%s-%s未找到匹配的记录", nf,bxgsmc,qhdm,zwmc));
            return true;
        }
        generateWord(dirPath, area, inspectionReportList.get(qhdm+zwmc), zwmc, nf);
        return true;
    }
    private void generateWord(String path, AreaDto area, List<Map<String, Object>> inspectionReportList, String zwmc,String nf){
        String inspectionSurveyWordFileName = nf+area.getCountyName() + area.getTownName()+area.getVillageName()+zwmc+"查验报告单.docx";
        if(isAll){
//            map.put("sffh",old.get("sffh"));
//            map.put("tbdh",old.get("tbdh"));
//            map.put("bz",old.get("bz"));
//            map.put("nhxm",old.get("nhxm"));
//            map.put("tbmj",old.get("tbmj"));

            //inspectionReportList根据nhxm字段汇总，tbmj取和
            for (int i = 0; i < inspectionReportList.size(); i++) {
                Map<String, Object> old = inspectionReportList.get(i);
                for (int j = i+1; j < inspectionReportList.size(); j++) {
                    Map<String, Object> newMap = inspectionReportList.get(j);
                    if(old.get("nhxm").equals(newMap.get("nhxm"))){
                        old.put("tbmj",Double.parseDouble(old.get("tbmj").toString())+Double.parseDouble(newMap.get("tbmj").toString()));
                        inspectionReportList.remove(j);
                        j--;
                    }
                }
            }
        }
        Map<String, Object> wordParam = WordUtils.transferParam(area.getCountyName() + area.getTownName() + area.getVillageName(), zwmc, area.getVillageName(), inspectionReportList);

        WordUtils.saveWord(path+ File.separator + inspectionSurveyWordFileName,wordParam ,"inspectionSurveyV2");
//        List lists = (List) wordParam.get("list");
//        Map<String, Object> inspectionReportParam = WordUtils.transferInspectionReportParam(area.getCountyName() + area.getTownName() + area.getVillageName(), tbdh, tbr, zwmc, area.getVillageName(), zltCount,  lists.size(), farmerCount);
//        String inspectionReportWordFileName = "种植业保险查验报告单.docx";
//        WordUtils.saveWord(path + inspectionReportWordFileName, inspectionReportParam,"inspectionReportV2");
    }
}
